package com.payment.ctl;

import com.payment.client.response.PaymentResponse;
import com.payment.client.request.PaymentRequest;
import com.payment.service.PaymentProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * dongzhihua
 * 2022/10/20 18:08
 */
@Slf4j
@RestController
@RequestMapping("payment")
public class PaymentCtl {

    @Value("${server.port:0000}")
    private String port;
    @Resource
    private PaymentProvider paymentProvider;

    @RequestMapping("create")
    public String pay(PaymentRequest paymentRequest) {
        log.info("pay {}", paymentRequest);
        paymentProvider.send("payment create success orderNo=" + paymentRequest.getOrderNo());
        return String.format("payment create success orderNo=%s port=%s", paymentRequest.getOrderNo(), port);
    }

    @RequestMapping("get")
    public PaymentResponse get(@RequestBody PaymentRequest paymentRequest) {
        log.info("get {}", paymentRequest);
        PaymentResponse result = new PaymentResponse();
        result.setCode(1);
        result.setMsg("成功");
        result.setPort(port);
        result.setOrderNo(paymentRequest.getOrderNo());
        return result;
    }
    @RequestMapping("list")
    public PaymentResponse list() {
        PaymentResponse result = new PaymentResponse();
        result.setCode(1);
        result.setMsg("成功");
        result.setPort(port);
        result.setOrderNo("x001");
        return result;
    }

    @RequestMapping("timeout")
    public PaymentResponse timeout(@RequestBody PaymentRequest paymentRequest) throws InterruptedException {
        log.info("get {}", paymentRequest);
        PaymentResponse result = new PaymentResponse();
        result.setCode(1);
        result.setMsg("成功");
        result.setPort(port);
        result.setOrderNo(paymentRequest.getOrderNo());
        TimeUnit.SECONDS.sleep(5);
        return result;
    }
}
